<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">所属单位</label>
        <el-select v-model="query.dealer_id" filterable placeholder="请选择" style="width: 185px;" class="filter-item">
          <el-option v-for="item in dict.dealers" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <label class="el-form-item-label">农机制造编号</label>
        <el-input
          v-model="query.eq_code"
          clearable
          placeholder="农机制造编号"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">农机型号</label>
        <el-input
          v-model="query.eq_module"
          clearable
          placeholder="农机型号"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" >
        <el-popover
            slot="right"
            placement="bottom-start"
            width="390"
            v-model="uploadFilePop"
            @hide="excelUploadHide"
        >
          <el-button
              slot="reference"
              v-if="crud.optShow.upload"
              class="filter-item"
              size="mini"
              type="danger"
              icon="el-icon-upload2"
          >导入</el-button>
          <el-upload
              ref="excelUpload"
              class="upload-demo"
              drag
              action="https://jsonplaceholder.typicode.com/posts/"
              :auto-upload="false"
              :limit = "1"
              :multiple="false"
              accept=".xls, .xlsx"
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">只能上传excel文件</div>
          </el-upload>
          <div style="text-align: right; margin: 0">
            <el-button size="mini" type="text" @click="uploadFilePop=false">取消</el-button>
            <el-button type="primary" :loading="uploadFileLoading" size="mini" @click="doUpload">确定</el-button>
          </div>
        </el-popover>
      </crudOperation>
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="840px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-row>
            <el-col  :span="12">
              <el-form-item label="所属单位" prop="dealer_id">
                <el-select
                    v-model="form.dealer_id"
                    filterable
                    placeholder="请选择"
                    style="width: 250px;"
                    @change="changeDealer"
                >
                  <el-option v-for="item in dict.dealers" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所属农机" prop="eq_id">
                <el-select v-model="form.eq_id" filterable placeholder="请选择" style="width: 250px;">
                  <el-option v-for="item in eqs" :key="item.id" :label="item.eq_module" :value="item.id" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="终端编号">
                <el-input v-model="form.terminal_code" style="width: 250px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
<!--              <el-form-item label="终端类型" prop="eq_type">-->
<!--                <el-select v-model="form.terminal_type" filterable placeholder="请选择" style="width: 250px;">-->
<!--                  <el-option v-for="item in dict.eq_terminal_type" :key="item.id" :label="item.label" :value="item.value" />-->
<!--                </el-select>-->
<!--              </el-form-item>-->
              <el-form-item label="终端类型">
                <el-input v-model="form.terminal_type" style="width: 250px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="终端品牌">
                <el-input v-model="form.terminal_pinpai" style="width: 250px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="终端型号">
                <el-input v-model="form.terminal_module" style="width: 250px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="终端供应商">
                <el-input v-model="form.terminal_supplier" style="width: 250px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">

            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="imei号">
                <el-input v-model="form.imei_no" style="width: 250px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="sim号码">
                <el-input v-model="form.sim_no" style="width: 250px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="SIM卡类型">
                <el-input v-model="form.sim_type" style="width: 250px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="流量类型">
                <el-input v-model="form.flow_type" style="width: 250px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="服务开始时间">
                <el-date-picker
                    v-model="form.service_start_date"
                    type="datetime"
                    style="width: 250px;"
                    value-format="yyyy-MM-dd"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="服务到期时间">
                <el-date-picker
                    v-model="form.service_end_date"
                    type="datetime"
                    style="width: 250px;"
                    value-format="yyyy-MM-dd"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="流量开始时间">
                <el-date-picker
                    v-model="form.flow_start_date"
                    type="datetime"
                    style="width: 250px;"
                    value-format="yyyy-MM-dd"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="流量到期时间">
                <el-date-picker
                    v-model="form.flow_end_date"
                    type="datetime"
                    style="width: 250px;"
                    value-format="yyyy-MM-dd"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="当前状态">
                <el-input v-model="form.current_status" style="width: 250px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="接口类型">
                <el-input v-model="form.interface_type" style="width: 250px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="拆下次数">
                <el-input v-model="form.take_down_count" style="width: 250px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="运营商">
                <el-input v-model="form.operator" style="width: 250px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="ICCID">
                <el-input v-model="form.iccid_no" style="width: 250px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="剩余流量">
                <el-input v-model="form.surplus_flow" style="width: 250px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="备注">
                <el-input v-model="form.remark" style="width: 250px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">

            </el-col>
          </el-row>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="dealer_name" label="所属单位" width="200" />
        <el-table-column prop="eq_code" label="农机制造编号" width="100" />
        <el-table-column prop="terminal_code" label="终端编号" width="100" />
        <el-table-column prop="eq_module" label="农机型号" width="150" />
        <el-table-column prop="terminal_type" label="终端类型" width="100" />
        <el-table-column prop="terminal_pinpai" label="终端品牌" width="100" />
        <el-table-column prop="terminal_module" label="终端型号" width="100" />
        <el-table-column prop="terminal_supplier" label="终端供应商" width="100" />
        <el-table-column prop="imei_no" label="imei号" width="100" />
        <el-table-column prop="sim_no" label="sim号码" width="100"/>
        <el-table-column prop="sim_type" label="SIM卡类型" width="100"/>
        <el-table-column prop="flow_type" label="流量类型" width="100"/>
        <el-table-column prop="service_start_date" label="服务开始时间" width="100" />
        <el-table-column prop="service_end_date" label="服务到期时间" width="100" />
        <el-table-column prop="flow_start_date" label="流量开始时间" width="100" />
        <el-table-column prop="flow_end_date" label="流量到期时间" width="100" />
        <el-table-column prop="current_status" label="当前状态" width="100"/>
        <el-table-column prop="interface_type" label="接口类型" width="100"/>
        <el-table-column prop="take_down_count" label="拆下次数" width="100"/>
        <el-table-column prop="operator" label="运营商" width="100"/>
        <el-table-column prop="iccid_no" label="ICCID" width="100"/>
        <el-table-column prop="surplus_flow" label="剩余流量" width="100"/>
        <el-table-column prop="remark" label="备注" />
        <el-table-column
          v-if="checkPer(['admin', 'equipmentTerminal:edit', 'equipmentTerminal:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
/** 农机终端信息  */
import crudEquipmentTerminal from '@/api/jf/equipmentTerminal'
import CRUD, { crud, form, header, presenter } from '@crud/V2/crud'
import crudOperation from '@crud/V2/CRUD.operation'
import pagination from '@crud/V2/Pagination'
import rrOperation from '@crud/V2/RR.operation'
import udOperation from '@crud/V2/UD.operation'

const defaultForm = {
  id: null,
  dealer_id: null,
  eq_id: null,
  terminal_code: null,
  terminal_type: null,
  terminal_pinpai: null,
  terminal_module: null,
  terminal_supplier: null,
  imei_no: null,
  sim_no: null,
  sim_type: null,
  flow_type: null,
  service_start_date: null,
  service_end_date: null,
  flow_start_date: null,
  flow_end_date: null,
  current_status: null,
  take_down_count: null,
  operator: null,
  iccid_no: null,
  surplus_flow: null,
  remark: null
}
export default {
  name: 'EquipmentTerminal',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '农机终端信息',
      url: 'api/v2/base/equip/equipmentTerminal',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudEquipmentTerminal }
    })
  },
  dictsV2: ['dealers'],
  dicts: ['eq_terminal_type'],
  data() {
    return {
      permission: {
        add: ['admin', 'equipmentTerminal:add'],
        edit: ['admin', 'equipmentTerminal:edit'],
        del: ['admin', 'equipmentTerminal:del']
      },
      rules: {
        dealer_id: [{ required: true, message: '不能为空', trigger: 'blur' }],
        eq_id: [{ required: true, message: '不能为空', trigger: 'blur' }]
      },
      queryTypeOptions: [
        { key: 'dealer_id', display_name: '所属单位' },
        { key: 'eq_code', display_name: '农机制造编号' },
        { key: 'eq_module', display_name: '农机型号' }
      ],
      eqs: [],
      uploadFilePop: false,
      uploadFileLoading: false
    }
  },
  created: function() {
    this.crud.optShow.upload = true
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeToEdit](crud, form) {
      this.getEqs(form.dealer_id, true)
    },
    // 获取设备
    getEqs(dealer_id, isNoClear) {
      if (dealer_id != null) {
        crudEquipmentTerminal
          .getEqListByDealer(dealer_id)
          .then(res => {
            this.eqs = res.data
          })
          .catch(() => {})
      } else {
        this.eqs = []
      }
      if (!isNoClear) {
        this.form.eq_id = ''
      }
    },
    changeDealer(value) {
      this.getEqs(value)
    },
    doUpload: function() {
      var files = this.$refs.excelUpload.uploadFiles
      if (files.length < 1) {
        this.crud.notify('请上传文件!', CRUD.NOTIFICATION_TYPE.ERROR)
        return
      }
      var params = {}
      params.excelFile = files[0].raw

      this.uploadFileLoading = true
      crudEquipmentTerminal.excelUpload(params).then(res => {
        if (res.code === 1) {
          this.$notify({
            title: '上传成功',
            type: 'success',
            duration: 2500
          })
          this.crud.refresh()
        } else {
          this.crud.notify('上传失败!', CRUD.NOTIFICATION_TYPE.ERROR)
        }

        this.uploadFileLoading = false
        this.uploadFilePop = false
      }).catch(err => {
        this.uploadFileLoading = false
        console.log(err.response.data.message)
        this.uploadFilePop = false
      })
    },

    excelUploadHide: function() {
      this.$refs.excelUpload.clearFiles()
    }
  }
}
</script>

<style scoped></style>
